package com.sms.common.persistence;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

/**
 * @desc filter组装
 * @author WuJiangWu
 * @date 2016年7月21日上午1:42:15
 */
public class DynamicSpecifications {

	/**
	 * @param filters
	 * @param entityClazz
	 * @return
	 */
	public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) {
		
		return new Specification<T>() {

			@Override
			public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				if (DynamicSpecifications.isNotEmpty(filters)) {

					List<Predicate> predicates = new ArrayList<Predicate>();
					for (SearchFilter filter : filters) {
						// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
						String[] names = StringUtils.split(filter.fieldName, ".");
						Path expression = root.get(names[0]);
						for (int i = 1; i < names.length; i++) {
							expression = expression.get(names[i]);
						}

						// logic operator
						switch (filter.operator) {
                            case EQ:
                                predicates.add(builder.equal(expression, filter.value));
                                break;
                            case NEQ:
                                predicates.add(builder.notEqual(expression, filter.value));
                                break;
                            case LIKE:
                                predicates.add(builder.like(expression, "%" + filter.value + "%"));
                                break;
                            case LLIKE:
                                predicates.add(builder.like(expression, filter.value + "%"));
                                break;
                            case RLIKE:
                                predicates.add(builder.like(expression, "%" + filter.value));
                                break;
                            case GT:
                                predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                                break;
                            case LT:
                                predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                                break;
                            case GTE:
                                predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                                break;
                            case LTE:
                                predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                                break;
						}
					}

					// 将所有条件用 and 联合起来
					if (predicates.size() > 0) {
						return builder.and(predicates.toArray(new Predicate[predicates.size()]));
					}
				}

				return builder.conjunction();
			}
		};
	}
	
	/**
	 * @param leftFilters
	 * @param rightFilters
	 * @param entityClazz
	 * @return
	 */
	public static <T> Specification<T> or(final Collection<SearchFilter> leftFilters,final Collection<SearchFilter> rightFilters, final Class<T> entityClazz) {
		return new Specification<T>() {
			@Override
			public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				if (DynamicSpecifications.isNotEmpty(leftFilters) && DynamicSpecifications.isNotEmpty(rightFilters)) {

					List<Predicate> leftPredicates = new ArrayList<Predicate>();
					for (SearchFilter filter : leftFilters) {
						// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
						String[] names = StringUtils.split(filter.fieldName, ".");
						Path expression = root.get(names[0]);
						for (int i = 1; i < names.length; i++) {
							expression = expression.get(names[i]);
						}

						// logic operator
						switchFilter(builder, leftPredicates, filter, expression);
					}
					
					List<Predicate> rightPredicates = new ArrayList<Predicate>();
					for (SearchFilter filter : rightFilters) {
						// nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
						String[] names = StringUtils.split(filter.fieldName, ".");
						Path expression = root.get(names[0]);
						for (int i = 1; i < names.length; i++) {
							expression = expression.get(names[i]);
						}

						// logic operator
						switchFilter(builder, rightPredicates, filter, expression);
					}

					// 将所有条件用 and 联合起来
					//if (predicates.size() > 0) {
					Predicate leftPred=builder.and(leftPredicates.toArray(new Predicate[leftPredicates.size()]));
					Predicate rightPred=builder.and(rightPredicates.toArray(new Predicate[rightPredicates.size()]));
					return builder.or(leftPred,rightPred);
					//}
				}

				return builder.conjunction();
			}

			private void switchFilter(CriteriaBuilder builder,
					List<Predicate> predicates, SearchFilter filter,
					Path expression) {
				switch (filter.operator) {
				    case EQ:
				        predicates.add(builder.equal(expression, filter.value));
				        break;
				    case NEQ:
				        predicates.add(builder.notEqual(expression, filter.value));
				        break;
				    case LIKE:
				        predicates.add(builder.like(expression, "%" + filter.value + "%"));
				        break;
				    case LLIKE:
				        predicates.add(builder.like(expression, filter.value + "%"));
				        break;
				    case RLIKE:
				        predicates.add(builder.like(expression, "%" + filter.value));
				        break;
				    case GT:
				        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
				        break;
				    case LT:
				        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
				        break;
				    case GTE:
				        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
				        break;
				    case LTE:
				        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
				        break;
				}
			}
		};
	}
	
	/**
	 * leftFilters和rightFilters为and关系，leftFilters里子条件为and关系，rightFilters里子条件为or关系
	 * 是实现搜索群组的需求而写的方法
	 * @param leftFilters
	 * @param rightFilters
	 * @param entityClazz
	 * @return
	 */
	public static <T> Specification<T> bothAndOr(final Collection<SearchFilter> leftFilters,final Collection<SearchFilter> rightFilters, final Class<T> entityClazz) {
        return new Specification<T>() {
            @Override
            public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            	leftFilters.isEmpty();
                if (DynamicSpecifications.isNotEmpty(leftFilters) && DynamicSpecifications.isNotEmpty(rightFilters)) {

                    List<Predicate> leftPredicates = new ArrayList<Predicate>();
                    for (SearchFilter filter : leftFilters) {
                        // nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
                        String[] names = StringUtils.split(filter.fieldName, ".");
                        Path expression = root.get(names[0]);
                        for (int i = 1; i < names.length; i++) {
                            expression = expression.get(names[i]);
                        }

                        // logic operator
                        switchFilter(builder, leftPredicates, filter, expression);
                    }
                    
                    List<Predicate> rightPredicates = new ArrayList<Predicate>();
                    for (SearchFilter filter : rightFilters) {
                        // nested path translate, 如Task的名为"user.name"的filedName, 转换为Task.user.name属性
                        String[] names = StringUtils.split(filter.fieldName, ".");
                        Path expression = root.get(names[0]);
                        for (int i = 1; i < names.length; i++) {
                            expression = expression.get(names[i]);
                        }

                        // logic operator
                        switchFilter(builder, rightPredicates, filter, expression);
                    }

                    // 将所有条件用 and 联合起来
                    //if (predicates.size() > 0) {
                    Predicate leftPred=builder.and(leftPredicates.toArray(new Predicate[leftPredicates.size()]));
                    Predicate rightPred=builder.or(rightPredicates.toArray(new Predicate[rightPredicates.size()]));
                    return builder.and(leftPred,rightPred);
                    //}
                }

                return builder.conjunction();
            }

            private void switchFilter(CriteriaBuilder builder,
                    List<Predicate> predicates, SearchFilter filter,
                    Path expression) {
                switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case NEQ:
                        predicates.add(builder.notEqual(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case LLIKE:
                        predicates.add(builder.like(expression, filter.value + "%"));
                        break;
                    case RLIKE:
                        predicates.add(builder.like(expression, "%" + filter.value));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                }
            }
        };
    }

	/**
     * 判断是否为空.
     * 
     * @param collection xx
     * @return xx
     */
    public static boolean isNotEmpty(Collection collection) {
        return ((collection != null) && !(collection.isEmpty()));
    }
}
